package com.akk.platform.manage;

import com.akk.module.avs.entity.DirectSalesBank;
import com.akk.module.avs.model.request.DirectSalesBankRequest;
import com.akk.module.avs.service.DirectSalesBankService;
import com.akk.module.upload.service.PictureService;
import com.akk.utils.PageUtils;
import com.akk.utils.Query;
import com.akk.utils.RestResponse;
import com.akk.utils.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "直销银行", description = "直销银行管理接口")
@RequestMapping(value = "/platform/manage/directSalesBank")
public class DirectSalesBankAction {

    @Autowired
    private DirectSalesBankService directSalesBankService;
    @Autowired
    private PictureService pictureService;

    @RequestMapping(value = "/{appId}/list", method = RequestMethod.GET)
    @ApiOperation(value = "分页查询")
    @RequiresPermissions("sys:directSalesBank:list")
    public RestResponse pageList(@PathVariable String appId, @Valid Query query) {
        PageHelper.startPage(query.getPage(), query.getLimit());
        List<DirectSalesBank> list = directSalesBankService.findByAppId(appId);
        return RestResponse.build(PageUtils.buildPage(list, (int) new PageInfo<>(list).getTotal(), query));
    }

    @RequestMapping(value = "/{appId}/insert", method = RequestMethod.POST)
    @ApiOperation(value = "添加数据")
    @RequiresPermissions("sys:directSalesBank:insert")
    public RestResponse insert(DirectSalesBankRequest directSalesBank, @PathVariable String appId) {
        if (directSalesBank.getFile() == null)
            throw new BusinessException("请上传文件");
        Map result = pictureService.uploadPicture(directSalesBank.getFile());
        if (Integer.valueOf(result.get("status").toString()) == 1) {
            throw new BusinessException("文件上传失败");
        }
        directSalesBank.setImage(result.get("url").toString());
        directSalesBank.setAppId(appId);
        directSalesBankService.insert(directSalesBank);
        return RestResponse.build("success");
    }

    @RequestMapping(value = "/{appId}/update", method = RequestMethod.POST)
    @ApiOperation(value = "修改数据")
    @RequiresPermissions("sys:directSalesBank:update")
    public RestResponse update(DirectSalesBankRequest directSalesBank, @PathVariable String appId) {
        if (!directSalesBank.getAppId().equals(appId))
            throw new BusinessException("非法操作");
        DirectSalesBank salesBank = directSalesBankService.findById(directSalesBank.getId());
        if (StringUtils.hasText(directSalesBank.getAppId()))
            salesBank.setUrl(directSalesBank.getAppId());
        if (StringUtils.hasText(directSalesBank.getBankStatus()))
            salesBank.setBankStatus(directSalesBank.getBankStatus());
        if (StringUtils.hasText(directSalesBank.getShowName()))
            salesBank.setShowName(directSalesBank.getShowName());
        if (StringUtils.hasText(directSalesBank.getUrl()))
            salesBank.setUrl(directSalesBank.getUrl());
        salesBank.setBankIndex(directSalesBank.getBankIndex());
        if (directSalesBank.getFile() != null) {
            Map result = pictureService.uploadPicture(directSalesBank.getFile());
            if (Integer.valueOf(result.get("status").toString()) == 1) {
                throw new BusinessException("文件上传失败");
            }
            pictureService.removeFile(salesBank.getImage());
            salesBank.setImage(result.get("url").toString());
        }
        directSalesBankService.update(salesBank);
        return RestResponse.build("success");
    }

    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "获取详情")
    @RequiresPermissions("sys:directSalesBank:info")
    public RestResponse info(@PathVariable Integer id) {
        return RestResponse.build(directSalesBankService.findById(id));
    }
}
